<?php
  //============================================================================
  // ____________________________PLIGG TEAM ADDON_______________________________
  //
  // This file was created by AnAlienHolakres3  ||| XXXXX  XX     XX XXXX   XXXX
  // (c) 2007-2008                Jakub Holasek ||| XXXXX  XX     XX X      X
  // @last_update                    22/02/2008 ||| XX     XX     XX X  XX  X  XX
  // @version                              0.70 ||| XX     XXXXXX XX XXXXX  XXXXX
  //
  // This file can be used by Pligg community.
  //=============================================================================
  
  include_once('Smarty.class.php');
  
  $main_smarty = new Smarty;
  
  include('config.php');
  include(mnminclude . 'html1.php');
  include(mnminclude . 'ts.php');
  include(mnminclude . 'link.php');
  include(mnminclude . 'tags.php');
  include(mnminclude . 'user.php');
  include(mnminclude . 'team_config.php');
  include(mnminclude . 'team.php');
  include(mnminclude . 'team_member.php');
  include(mnminclude . 'task.php'); 
  include(mnminclude . 'taskcomment.php');
  include(mnminclude . 'taskrating.php');
  include(mnminclude . 'taskfiles.php');
  include(mnminclude . 'tasklinkship.php');
  include(mnminclude . 'teamshoutbox.php'); 
  include(mnminclude . 'teamlogs.php'); 
  include(mnminclude . 'smartyvariables.php');
  
  
  // If not logged in, redirect to the index page
  if ($current_user->user_id > 0 && $current_user->authenticated) {
      $login = $current_user->user_login;
  } else {
      header('Location: ./login.php?return=/team.php');
      die;
  }
  
  // if there is request for ordering or filtering remember values
  if (isset($_GET['sort_filter_request'])) {
      $team = new Team;
      if ($team->check_sorting()) {
          $main_smarty->assign('team_sorting', $_GET['sortby']);
      }
      if ($team->check_filtering()) {
          $main_smarty->assign('team_filtering', $_GET['filterby']);
      }
  }
  
    // if there is request for ordering or filtering TASKs
  if (isset($_GET['sort_filter_task_request'])) {
      $task= new Task;
      $req = "&sort_filter_task_request=".$_GET['sort_filter_task_request'];
      if ($task->check_sorting()) { 
          $main_smarty->assign('task_sorting', $_GET['sorttaskby']);
           $sorting=true;
             $sort="&sorttaskby=".$_GET['sorttaskby'];
      }
      if ($task->check_filtering()) { 
          $main_smarty->assign('task_filtering', $_GET['filtertaskby']);
          $filtering=true;
           $filter="&filtertaskby=".$_GET['filtertaskby'];
      }
  }
  
  //if there is request for team CREATION
  if ($_POST['team_request']) {
      //if user entered correct values
      if (check_team_creation_values('save')) {
          //if there is an array it means errors during saving occured
          if (is_array($possible_error = save_team_values('save'))) {
              $team_error = $possible_error;              
              operation_failed($team_error, 'new_team');
              //if saving is successfull
          } else {
              $team_confirmation[] = 1;
              operation_successfull($team_confirmation);
              
          }
      }
  }
  
  //if there is request for NEW TEAM MEMBER
  if ($_POST['add_member_request']) {
      //if user entered correct values
      $team_member = new Team_Member;
      $team = new Team;
      
      if ($team->check_id($_POST['team_id'])) {
          //check authorization for saving         
          $team->set_id($_POST['team_id']);                     
          
          if ($team->is_team_creator()) {
              if (check_member_creation_values()) {
                  //if there is an array it means errors during saving occured
                  if (is_array($possible_error = save_team_member())) {
                      $team_error[] = $possible_error;
                      operation_failed($team_error, 'new_member');
                      //if saving is successfull
                  } else {
                      //$team->refresh_team_modification($_POST['team_id']);
                      // if triggers cannot be performed
                      $teamlog = new TeamLog;
                      $teamlog->save_teamlog_values(1,null,$team_member->get_user_id(),null);
                      $team_confirmation[] = 3;
                      operation_successfull($team_confirmation);
                      
                  }
              }
          } else {
              $team_error[] = 2;
              operation_failed($team_error, 'new_member');
          }
      }
  }
  
  
  
  // there is request of TEAM MEMBER DELETION
  if (isset($_POST['deluser']) && team_is_selected() && no_other_requests()) {
      //if user entered correct values
      $team_member = new Team_Member;
      $team = new Team;
      
      if ($team->check_id($_REQUEST['id'])) {
          //check authorization for user deletion
          $team->set_id($_REQUEST['id']);
          $team_member->set_team_id($_REQUEST['id']);
          
          if ($team->is_team_creator()) {
             // check if user´s id is NOT team creator id
             if ($team->user_is_not_team_creator($_POST['deluser'])) { 
              // checks if user´s id is team member ´s id
              if ($team_member->check_member_id($_POST['deluser'])) {
                  //if there is an array it means errors during saving occured                  
                  $team_member->set_user_id($_POST['deluser']);                                    
                      if (!$team_member->delete_member()) {
                            $team_error[] = 1;
                             operation_failed($team_error, 'delete_member');
                            //if saving is successfull
                      } else {
                            $teamlog = new TeamLog;
                            $teamlog->save_teamlog_values(2,null,$_POST['deluser'],null);
                            $team->refresh_team_modification($_REQUEST['id']);
                             $team_confirmation[] = 4;
                              operation_successfull($team_confirmation);    
                     }
                 
              } else {
                  $team_error[] = 3;
                   operation_failed($team_error, 'delete_member');
              }
             } else { //creator deletion     
             
                $team_error[] = 7;
                operation_failed($team_error, 'delete_member');
                 
             }
          } else {
              $team_error[] = 2;
               operation_failed($team_error, 'delete_member');
          }
      }
  }
  
    // there is request of TEAM TASK DELETION
  if (isset($_POST['deltask']) && team_is_selected() && no_other_requests()) {
      //if user entered correct values
      $task = new Task;
      $team = new Team;
      
      if ($team->check_id($_REQUEST['id']) && $team->check_id($_POST['deltask'])) {
          //check authorization for user deletion
          $team->set_id($_REQUEST['id']);
          $task->set_task_team_id($_REQUEST['id']);
          
          // check if user´s id is NOT team creator id
          if ($team->is_team_creator()) {

                  //if there is an array it means errors during saving occured                  
                  $task->set_task_id($_POST['deltask']);
                     if ($task->specific_task_exist()) {
                         
                         $teamlog = new TeamLog;                         
                         $teamlog->save_teamlog_values(4,$_POST['deltask'],$task->get_task_worker(),$task->get_task_name());   
                         
                      if (!$task->delete_task()) {
                            $team_error[] = 27;
                             operation_failed($team_error, 'delete_task');
                            //if saving is successfull
                      } else {                                                                       
                            $team->refresh_team_modification($_REQUEST['id']);
                             $team_confirmation[] = 7;
                              operation_successfull($team_confirmation);    
                     }
                  }else {
                  $team_error[] = 28;
                  operation_failed($team_error, 'delete_task');
                  }              
          } else {
              $team_error[] = 10;
               operation_failed($team_error, 'delete_task');
          }
      }
  }
  
  //if there is request for team UPDATE  
  if ($_POST['update_team_request'] && team_is_selected()) {
      //if user entered correct values
      if (check_team_creation_values('update')) {
          //check authorization for saving
          $team = new Team;
          $team->set_id($_POST['team_id']);
          
          if ($team->is_team_creator()) {
              //if there is an array it means errors during saving occured
              if (is_array($possible_error = save_team_values('update'))) {
                  $team_error[] = $possible_error;
                  operation_failed($team_error, 'update');
                  //if saving is successfull
              } else {
                 $teamlog = new TeamLog;                          
                 $teamlog->save_teamlog_values(12,null,$current_user->user_id,null);
                  $team->refresh_team_modification($_POST['team_id']);
                   $team_confirmation[] = 2;
                    operation_successfull($team_confirmation);
                    
              }
          } else {
              $team_error[] = 11;
               operation_failed($team_error, 'update');
          }
      }
  }
  
    //if there is request for team DELETION  
  if ($_POST['delteam'] && team_is_selected()) {
      $team = new Team;
      //if user entered correct values
       if ($team->check_id($_REQUEST['id']) && $team->check_id($_POST['delteam'])) {               
          $team->set_id($_POST['delteam']);                              
          if ($team->is_team_creator()) {      
             
              if (!$team->delete_team()) {
                  $team_error[] =1;
                  operation_failed($team_error, 'delete');
                  //if saving is successfull
              } else {
                    
                    $team_confirmation[] = 2;
                    operation_successfull($team_confirmation);               
                    $deleted=true;
                    header('Location: ./login.php?return=/team.php');

              }
          } else {
              $team_error[] = 2;
               operation_failed($team_error, 'delete');
          }
       }
  }
  
    //if there is request for NEW TEAM TASK
  if ($_POST['add_team_task'] && team_is_selected()) {  
      //if user entered correct values
      $task = new Task;
      $team = new Team;
      
      if ($team->check_id($_REQUEST['id'])) {
          //check authorization for saving                   
          $task->set_task_team_id($_REQUEST['id']);
          $team->set_id($_REQUEST['id']);
                     
          if ($team->is_team_creator()) {    
             if (check_task_creation_values('save')) {
              //if there is an array it means errors during saving occured
                if (is_array($possible_error = $task->save_task_values('save'))) {   
                             $team_error = $possible_error;
                             operation_failed($team_error, 'new_task');
                //if saving is successfull
              } else {  
                       $teamlog = new TeamLog;
                       $teamlog->save_teamlog_values(3,null,$current_user->user_id,null);    
                       $team->refresh_team_modification($_REQUEST['id']);
                       $team_confirmation[] = 5;
                       operation_successfull($team_confirmation);
                  }           
              }             
           } else {
              $team_error[] = 8;
              operation_failed($team_error, 'new_task');
          
          }
      }
  }
  
    //if there is request for TEAM TASK EDIT
  if ($_POST['edit_task'] && team_is_selected() && no_other_requests()) {
      //if user entered correct values
            $task = new Task;
            $team = new Team;
            $taskrating = new Taskrating;
            
      if ($team->check_id($_REQUEST['id']) && $team->check_id($_POST['edit_task'])) {
          //check authorization for saving                   
          $task->set_task_team_id($_REQUEST['id']);         
          $team->set_id($_REQUEST['id']);
          $task->set_task_id($_POST['edit_task']); 
          
          if ($team->is_team_creator()) {
             $main_smarty->assign('show_task_edit', true);
              $main_smarty->assign('task_edit_id',$_POST['edit_task']);
               $task->read_basic();
                 $task->fill_print('basic'); 
                 
              if ($_POST['edit_task_request']) {
                  if (check_task_creation_values('update')) {
                    if ($task->specific_task_exist()) {
                        
                        $teamlog = new TeamLog;                         
                         $teamlog->save_teamlog_values(5,$_POST['edit_task'],$task->get_task_worker(),null);   
                         
                      //if there is an array it means errors during saving occured
                      if (is_array($possible_error = $task->save_task_values('update'))) {
                          $team_error = $possible_error;
                          operation_failed($team_error, 'task_update');                       
                          
                          //if saving is successfull
                      } else { 
                          $main_smarty->assign('show_task_edit', false);
                          $main_smarty->assign('taskedit_completed', true);
                          
                          $taskrating->set_task_id($_POST['edit_task']);
                          $taskrating->set_team_id($_REQUEST['id']);
                          $taskrating->set_user_id($_POST["task_worker"]);
                          $taskrating->remove_taskrating();
                          
                          $task->read_basic();
                          $task->fill_print('basic');                                                                           
                          $team->refresh_team_modification($_REQUEST['id']);
                          $task->refresh_task_modification($_POST['edit_task'], $_REQUEST['id']);
                          $team_confirmation[] = 6;
                          operation_successfull($team_confirmation);
                          $id = $_REQUEST['id'];
                        
                          
                      }
                  }else {
                  $team_error[] = 28;
                  operation_failed($team_error, 'task_update'); 
                  }
                }
              }             
              
          } else {
              $team_error[] = 9;
              operation_failed($team_error, 'task_update');
          }
      }
  }
  //  TASK completeness UPDATE request
  if ($_POST['task_completeness_update_request'] && team_is_selected() && $_POST['task_id']){
                    
      $team = new Team;
      $task = new Task;
      
       if ($team->check_id($_REQUEST['id']) && $team->check_id($_POST['task_id'])) {  
           $task->set_task_team_id($_REQUEST['id']);
           $task->set_task_id($_POST['task_id']);
           $task->read_basic();
           
            //check if the one who is updating is task worker
            if ($task->is_task_worker()){        
                if ($task->check_task_completeness($_POST['task_completeness'])){
                    $task->set_task_completeness($_POST['task_completeness']);
                     
                     if ($task->update_completeness()){
                         // MySQL allow to create only one trigger to operation,this must be coded
                          $teamlog = new TeamLog;                          
                          $teamlog->save_teamlog_values(11,$_POST['task_id'],$current_user->user_id,$_POST['task_completeness']);                                              
                          $team_confirmation[] = 6;
                          operation_successfull($team_confirmation);
                          
                          
                     } else {$team_error[]=12;operation_failed($team_error, 'task_completeness');}                                            
                } else {$team_error[]=14;operation_failed($team_error, 'task_completeness');}                                                          
           } else {$team_error[]=13;operation_failed($team_error, 'task_completeness');}            
       } 
  }

// if there is request for Task COMMENT ADDING 
if ($_POST['add_taskcomment_request']  && team_is_selected()){
    global $current_user;
       $team = new Team;
       $task = new Task;
       $taskComment = new TaskComment; 
       $team->check_id($_POST['task_id']);
       $team->set_id($_REQUEST['id']);
       
        //check authorization (team member including task worker and team creator)
        if ($team->is_team_member()){              
              if ($taskComment->check_taskcomment_content_exist()){
                  if ($taskComment->check_taskcomment_content_length()){
                      if ($taskComment->save_taskcomment_values()) {
                          
                          
                          $teamlog = new TeamLog;
                          $teamlog->save_teamlog_values(8,$_POST['task_id'],$current_user->user_id,null);
                          $team_confirmation[] = 8;
                          operation_successfull($team_confirmation);
                          
                          
                      } else {
                        $team_error[] = 12;
                        operation_failed($team_error, 'task_comment');        
                      }
                      
                  } else {
                  $team_error[] = 17;
                  operation_failed($team_error, 'task_comment');        
                  }
              } else {
              $team_error[] = 16;
              operation_failed($team_error, 'task_comment');     
              }
        } else {
              $team_error[] = 15;
              operation_failed($team_error, 'task_comment');
          }
        
        ////check ids and  content
        
}
  

// if there is request for Task RATING
if ($_POST['taskrating_request']  && team_is_selected()){
    global $current_user;
       $team = new Team;
       $task = new Task;
       $taskRating = new TaskRating;
       
       $team->check_id($_POST['task_id']);
       $team->set_id($_REQUEST['id']);
       $task->set_task_id($_POST['task_id']);
       $task->set_task_team_id($_REQUEST['id']);
       $task->read_basic();
       
       if ($taskRating->user_already_voted($_POST['task_id'],$_REQUEST['id'])){
        $user_voted = true;   
       }
       
        //check authorization (team member WITHOUT task worker)
        if ($team->is_team_member()){
            if (!$task->is_task_worker()){              
              if ($taskRating->check_taskrating_value($_POST['taskrating_value'])){                 
                    $taskRating->set_task_id($_POST['task_id']);
                    $taskRating->set_team_id($_REQUEST['id']);
                      if ($taskRating->save_taskrating()) {                          
                          
                          $teamlog = new TeamLog;
                          
                          if ($user_voted){
                            $teamlog->save_teamlog_values(7,$_POST['task_id'],$current_user->user_id,null);  
                          } else {                       
                             $teamlog->save_teamlog_values(6,$_POST['task_id'],$current_user->user_id,null);   
                          }
                          
                          
                         // $task->refresh_task_modification($_POST['task_id'], $_REQUEST['id']);
                          $team_confirmation[] = 9;
                          operation_successfull($team_confirmation);
                          
                      } else {
                        $team_error[] = 12;
                        operation_failed($team_error, 'task_rating');        
                      }                                      
              } else {
              $team_error[] = 19;
              operation_failed($team_error, 'task_rating');     
              }
            } else {
              $team_error[] = 18;
              operation_failed($team_error, 'task_rating'); 
            }
        } else {
              $team_error[] = 15;
              operation_failed($team_error, 'task_rating');
          }                       
    }  
      
  // if there is request for file UPLOAD
  if ($_POST['file_upload_request'] && team_is_selected()){
      $team = new Team;
      $task = new Task;
      $taskfile = new TaskFile;
        
      $team->check_id($_POST['task_id']);
      $team->set_id($_REQUEST['id']);
      $task->set_task_id($_POST['task_id']);
      $task->set_task_team_id($_REQUEST['id']);
      $task->read_basic();
        
   if ($task->is_task_worker()){   
       if (!$_FILES['taskfile']['error']){           
           if ($taskfile->check_taskfile_name($_FILES['taskfile']['name'])) {       
               if ($taskfile->check_taskfile_type($_FILES['taskfile']['name'])) {  
                   if ($taskfile->check_taskfile_size_min($_FILES['taskfile']['size'])){     
                       if ($taskfile->check_taskfile_size_max($_FILES['taskfile']['size'])){                                                  
                           if ($taskfile->save_taskfile()) {   
                               $taskfile->delete_old_revisions();
                               $task->refresh_task_modification($_POST['task_id'], $_REQUEST['id']);
                               
                               $teamlog = new TeamLog;
                               $teamlog->save_teamlog_values(10,$_POST['task_id'],$current_user->user_id,null);
                               $team_confirmation[] = 10;
                               operation_successfull($team_confirmation);
                               
                            } else {                                                
                              $team_error[] = 21;
                              operation_failed($team_error, 'task_file_upload');     
                              }                                                              
                       } else {
                         $team_error[] = 24;
                         operation_failed($team_error, 'task_file_upload');     
                        }
                   } else {
                     $team_error[] = 26;
                     operation_failed($team_error, 'task_file_upload');     
                     }                 
               } else {
                $team_error[] = 25;
                operation_failed($team_error, 'task_file_upload');     
                }  
           } else {
              $team_error[] = 22;
              operation_failed($team_error, 'task_file_upload');     
              } 
      } else {       
             operation_failed($taskfile->switch_file_error($_FILES['taskfile']['error']), 'task_file_upload');
         }
    } else {                                                  
      $team_error[] = 20;
      operation_failed($team_error, 'task_file_upload');              
              
    }     
  }
  
  // if there is request for file download
  
  if ($_POST['taskfile_download_request'] && team_is_selected()) {
    
   
      $taskfile = new TaskFile;
      $team = new Team;
     
      $team->check_id($_POST['taskfile_task_id']);
      $team->check_id($_POST['taskfile_id']);
           
      $taskfile->set_taskfile_task_id($_POST['taskfile_task_id']);
      $taskfile->set_taskfile_team_id($_REQUEST['id']);    
      $taskfile->set_taskfile_id($_POST['taskfile_id']);
     //authorization    
     if ($taskfile->can_i_download()) {
         $taskfile->update_taskfile_downloads();
         $taskfile->download_taskfile();         
           
      } else { operation_failed(null, 'taskfile_download');  }
     
  }
 
  // =================================================================
  // if specific team IS selected and user has authorization to access
  // =================================================================
  $team = new Team;
  
  if (team_is_selected() && !$deleted) {
      //get team data                             
      $team->set_id($_REQUEST['id']);
      $team->read(true);
        //access for Pligg moderators, to restrict comment that
        $canIhaveAccess = 0;
        $canIhaveAccess = $canIhaveAccess + checklevel('god');
        
      if ($team->check_team_access() || $canIhaveAccess == 1 ) {
          // get team,task,taskcomment,taskrating,taskfile,tasklinkship and teamshoutbox data
          $team_member = new Team_Member;
          $task = new Task;
          $taskComment = new TaskComment;
          $taskRating = new TaskRating;
          $taskfile = new TaskFile;  
          $tasklinkship = new TaskLinkhip;
          $shoutbox = new TeamShoutbox;
          
          $team_member->set_team_id($_REQUEST['id']);
          $team_member->read_members();
          $team_member->fill_print();
          
          $task->set_task_team_id($_REQUEST['id']);
          $task->read_tasks();
          
          $taskComment->set_taskcomment_team_id($_REQUEST['id']);
          $taskComment->read_taskcomments();
          $taskComment->fill_print();
          
          $taskRating->set_team_id($_REQUEST['id']);
          $taskRating->read_taskrating();
          $taskRating->fill_print();
          
          $taskfile->set_taskfile_team_id($_REQUEST['id']);  
          $taskfile->read_taskfiles();
          $taskfile->fill_print();   
          
          $tasklinkship->set_team_id($_REQUEST['id']);                         
          $tasklinkship->read_tasklinks();
          $tasklinkship->fill_print();
          
          $shoutbox->set_shoutbox_team_id($_REQUEST['id']);
          $shoutbox->read_team_shoutbox();
          $shoutbox->fill_print();
          
          // fill task smartys 
          $task->fill_print('full');
          
          //pagetitle
          set_breadcrumbs_pagetitle('one');
          
          // activate date  javascript functions
          $main_smarty->assign('require_date', true);
          $main_smarty->assign('require_shoutbox', true);
          
          // pagename
          define('pagename', 'theteam');
          
          
          // fill team smartys 
          $team->fill_print('full');
          
          
          //assign template for selected team
          $main_smarty->assign('tpl_center', $the_template . '/theteam_center');
      } else { $access_denied=true;}
  } 
  // ================================================================================
  // if specific team IS NOT selected or user has NO authorization or team is deleted
  // ================================================================================       
  if (!team_is_selected() || $access_denied || $deleted) {
      set_breadcrumbs_pagetitle('all');
      
      // pagename    
      define('pagename', 'team');
      
      // figure out what "page" of the results we're on (for team_center.tpl)
      $team = new Team;
      $team_page_size = $team->get_pagesize();
      $offset = (get_current_page() - 1) * $team_page_size;
        
      $team->read_basic_data($offset);
      // fill team smartys 
      $team->fill_print('basic');      
      
      //finds out if there is any team    
      if ($team->team_exist()) {
          $main_smarty->assign('team_exist', true);
      }
      
       
      //assign template for team list
      $main_smarty->assign('tpl_center', $the_template . '/team_center');
           
      
      // if user tried to access private team without access privilegia
      if (team_is_selected()) {
          $team->set_id($_REQUEST['id']);
          $team->read(true);
          
          if (!$team->check_team_access() && !$_POST['team_request'] && !$deleted) {
              // we do not need this warning message when team is created or deleted            
              $main_smarty->assign('unauthorized_access', true);
          }
      }
  } 
  //set rating
  $rating_array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  $main_smarty->assign('rating_array', $rating_array);
  //set categories
  
  include_once(mnminclude . 'dbtree.php');
  $array = tree_to_array(0, table_categories, false);
  
  $main_smarty->assign('lastspacer', 0);
  $main_smarty->assign('cat_array', $array);
  
  //define pagename
  $main_smarty->assign('pagename', pagename);
  
  // no sidebar, wider contentbox
  $main_smarty->assign('no_sidebar', true);
  
  //no header sorting
  $main_smarty->assign('no_headsort', true);
  
  // activate other team javascript functions in js library, this is used in pligg.tpl
  $main_smarty->assign('require_team', true);
  
  
  //display template 
  $main_smarty->display($the_template . '/pligg.tpl');
  
  
  
  //========================================================================
  // Function set_breadcrumbs_pagetitle()  sets breadcrumbs and pagetitle
  //========================================================================    
  function set_breadcrumbs_pagetitle($team_selection)
  {
      global $main_smarty;
      
      if ($team_selection == "one") {
          $team = new Team;
          $team->set_id($_REQUEST['id']);
          $team->read_name();
          $team_name = " " . $team->get_name();
          
          $navwhere['text1'] = $main_smarty->get_config_vars('PLIGG_Visual_Teams');
          
          $navwhere['link2'] = getmyurl('team');
          $navwhere['text2'] = $team_name;
          $main_smarty->assign('navbar_where', $navwhere);
          $main_smarty->assign('posttitle', " / " . $main_smarty->get_config_vars('PLIGG_Visual_Teams') . ' / ' . $thecat);
          $main_smarty->assign('page_header', $main_smarty->get_config_vars('PLIGG_Visual_Teams') . ' / ' . $thecat);
      }
      
      if ($team_selection == "all") {
          $navwhere['text1'] = $main_smarty->get_config_vars('PLIGG_Visual_Teams');
          $navwhere['link1'] = getmyurl('team');
          
          $main_smarty->assign('navbar_where', $navwhere);
          $main_smarty->assign('posttitle', " / " . $main_smarty->get_config_vars('PLIGG_Visual_Teams'));
          $main_smarty->assign('page_header', $main_smarty->get_config_vars('PLIGG_Visual_Teams'));
      }
  }
  
  
  


  
  //========================================================================
  // Function check_team_creation_values() checks if all neccessary values
  // are present, if no it display errors if yes, true is returned
  // 
  // @param $mode select if it is registration of new team or its update
  // @return true if no error was detected
  // @return false if there was an error
  //========================================================================    
  
  function check_team_creation_values($mode)
  {
      global $main_smarty;
      
      $team = new Team;
      
      if ($mode == "save") {
          // name existence
          if (!$team->check_name_exist()) {
              $team_error[] = 1;
          } else {
              // name length
              if (!$team->check_name_length()) {
                  $team_error[] = 2;
              }
          }
          //if update check id    
          } else
          {
              $team->check_id($_POST['team_id']);
          }
          
          // publicity
          if (!$team->check_publicity()) {
              $team_error[] = 3;
          }
          
          // category
          if (!$team->check_category()) {
              $team_error[] = 4;
          }
          
          // objective existence
          if (!$team->check_objective_exist()) {
              $team_error[] = 5;
          } else {
              // objective length
              if (!$team->check_objective_length()) {
                  $team_error[] = 6;
              }
          }
          
          // rating
          if (!$team->check_rating()) {
              $team_error[] = 7;
          }
          
          if ($team_error) {
              operation_failed($team_error, 'new_team');
              return false;
          }
          
          return true;
      }
      
      //========================================================================
      // Function save_team_values() creates new instance of team, sets values
      // and saves values to database
      //
      // @param $mode select if it is registration of new team or its update
      // @return ok if data are stored to database
      // @return error number if there was an error
      //========================================================================  
      
      function save_team_values($mode)
      {
          global $current_user;
          
          
          $team = new Team;
          if ($mode == "save") {    
              if ($team->duplicate_name($_POST['name'])) {     
                  //team name already exists  
                  $team_error[] = 9;
                  return $team_error;
              }
              //set team values
              $team->set_name($_POST['name']);
          } else {
              $team->set_id($_POST['team_id']);
          }
          
          $team->set_publicity($_POST['publicity']);
          $team->set_category($_POST['category']);
          $team->set_creator($current_user->user_id);
          $team->set_last_modify(time());
          $team->set_objective($_POST['objective']);
          $team->set_rating($_POST['rating']);
          $team->set_setup_date(time());
          
          //store to database
          if (!$team->store()) {  
              //cannot save to dabase
              $team_error[] = 8;
              return $team_error;
          }
          return true;
      }
 
     
           
  //========================================================================
  // Function check_task_creation_values() checks if all neccessary values
  // are present, if no it display errors if yes, true is returned
  // 
  // @param $mode select if it is registration of new task or its update
  // @return true if no error was detected
  // @return false if there was an error
  //========================================================================    
  
  function check_task_creation_values($mode)
  {
      global $main_smarty;
      
      $task = new Task;
      $team = new Team;
      
 
         //if update check id    
          if ($mode != "save") 
          {
              $team->check_id($_REQUEST['id']);
          }
              
          // task existence
          if (!$task->check_task_name_exists()) {
              $team_error[] = 2;
          } else {
              // task length
              if (!$task->check_task_name_length()) {
                  $team_error[] = 3;
              }
          }
                  
          // worker
          if (!$task->check_task_worker()){
              $team_error[] = 6;
          }
          
          // deadline
          if (!$task->check_task_deadline()) {
              $team_error[] = 7;
          }
          
          // description existence
          if (!$task->check_task_description_exists()) {
              $team_error[] = 4;
          } else {
              // description length
              if (!$task->check_task_desciption_length()) {
                  $team_error[] = 5;
              }
          } 
          
          if ($team_error) {
              if ($mode=="save"){
                operation_failed($team_error, 'new_task');
              } else {   
                operation_failed($team_error, 'task_update'); 
              }
              
              return false;
          }
          
          return true;
      }
      
      //========================================================================
      // Function save_team_member() creates new instance of team member, sets v
      // and saves values to database
      
      // @return ok if data are stored to database
      // @return error number if there was an error
      //========================================================================  
      
      function save_team_member()
      {
          global $current_user;
          
          $team_member = new Team_Member;
          
          //setting values
          $team_member->set_team_id($_POST['team_id']);
          $team_member->set_user_name($_POST["member_name"]);
          
          //store to database
          if (!$team_member->store()) {
              //cannot save to dabase
              $team_error = 8;
              return $team_error;
          }
          return true;
      }
      
      //========================================================================
      // Function check_member_creation_values() checks if all neccessary values
      // are present, if no it display errors if yes, true is returned
      // 
      // @return true if no error was detected
      // @return false if there was an error
      //========================================================================   
      function check_member_creation_values()
      {
          $team_member = new Team_Member;
          
          $team = new Team;
          $team->set_id($_POST['team_id']);
          
          if (!$team_member->check_team_member_name_length()) {
              $team_error[] = 3;
          } elseif (!$team_member->check_user_exist()) {
              $team_error[] = 4;
          } elseif ($team_member->is_duplicate_membership()) {
              $team_error[] = 5;
          } elseif ($team->get_team_creator() == $team_member->get_user_id()) {
              $team_error[] = 6;
          }
          if ($team_error) {
              operation_failed($team_error, 'new_member');
              return false;
          }
          
          return true;
      }
              
      
      //========================================================================
      // Function operation_successfull($confirmation) set smartys for displaying  
      // confirmation of successfull operation
      // @param  confirmation = array of ids confirmation messages 
      //========================================================================  
      
      function operation_successfull($team_confirmation)
      {
          global $main_smarty;
          
          $main_smarty->assign('team_confirmation', $team_confirmation);
          $main_smarty->assign('team_confirmation_present', true);
      }
      
      //========================================================================
      // Function operation_failed($error) set smartys for displaying  
      // errors of failed operation
      // @param  team_error = array of ids error messages 
      // @param team_error mode=  team creation,team update,user add....etc
      //========================================================================  
      
      function operation_failed($team_error, $team_error_mode)
      {
          global $main_smarty;
          
          //current values for team creation and update
          if ($team_error_mode == "new_team" || $team_error_mode == "update") {
              show_current_values('team');
              
              $main_smarty->assign('team_error', $team_error);
              $main_smarty->assign('team_error_present', true);
          }
          
          // team deletion
          if ($team_error_mode == "delete") {              
              
              $main_smarty->assign('team_error', $team_error);
              $main_smarty->assign('team_delete_error_present', true);
          }
          
          // current values for task creation
          if ($team_error_mode == "new_task") {
              show_current_values('task');
              
              $main_smarty->assign('team_error', $team_error);
              $main_smarty->assign('team_task_error_present', true);
          }
          // current values for task creation
          if ($team_error_mode == "delete_task") {              
              
              $main_smarty->assign('team_error', $team_error);
              $main_smarty->assign('team_task_delete_error_present', true);
          }
          
          // current values for task edit
          if ($team_error_mode == "task_update") {
              show_current_values('task_update');
              //only for edit task section              
              $main_smarty->assign('task_error_in_edit_section',true);
              
              $main_smarty->assign('team_error', $team_error);
              $main_smarty->assign('team_task_error_present', true);
          }
          // task completeness
          if ($team_error_mode == "task_completeness") {                           
              $main_smarty->assign('team_task_error_completeness', $team_error);   
              $main_smarty->assign('team_error', $team_error);         
          }
          // task comment
          if ($team_error_mode == "task_comment") {                           
              $main_smarty->assign('team_task_comment_error_present', $team_error);   
              $main_smarty->assign('team_error', $team_error);         
          }
          
          // task rating
          if ($team_error_mode == "task_rating") {                           
              $main_smarty->assign('team_task_rating_error_present', $team_error);   
              $main_smarty->assign('team_error', $team_error);         
          }
           // task file upload
          if ($team_error_mode == "task_file_upload") {                           
              $main_smarty->assign('team_task_file_error_present', $team_error);   
              $main_smarty->assign('team_error', $team_error);         
          }
          
          // task file download
          if ($team_error_mode == "taskfile_download") {                           
              $main_smarty->assign('taskfile_download_error_present',true);   
              
          }
          
          // new team member
          if ($team_error_mode == "new_member") {
              $main_smarty->assign('team_error', $team_error);
              $main_smarty->assign('team_user_error_present', true);
          }
          
          if ($team_error_mode == "delete_member") {
              $main_smarty->assign('team_error', $team_error);
              $main_smarty->assign('team_user_error_present', true);
          }
          
          if ($team_error_mode == "task_delete") {
              $main_smarty->assign('team_error', $team_error);
              $main_smarty->assign('team_task_error_present', true);
          }
          
          
          $main_smarty->assign('team_error_mode', $team_error_mode);
      }
      
      //========================================================================
      // Function show_current_values() assign already entered values, this is  
      // good for users if their team creation process failed, they do not have
      // to enter correct values (or options) again. This function also 
      // assign values for highlighting incorrect values
      //
      // @param type  - current team or task values
      //========================================================================  
      
      function show_current_values($type)
      {
          global $main_smarty;
          
          $team = new Team;
          $task = new Task;
          $task->set_task_team_id($_REQUEST['id']);  
          
  if ($type=="team"){
          // name existencwe and length
          if ($team->check_name_exist() && $team->check_name_length()) {
              $main_smarty->assign('team_name', $_POST['name']);
          } else {
              $main_smarty->assign('highlight_team_name', true);
          }
          
          
          // publicity
          if ($team->check_publicity()) {
              $main_smarty->assign('team_publicity', $_POST['publicity']);
          } else {
              $main_smarty->assign('highlight_team_publicity', true);
              //beceause there is ZERO value also one of select option we need to tell it exists
              $main_smarty->assign('team_publicity_select', true);
          }
          
          // category
          if ($team->check_category()) {
              $main_smarty->assign('team_category', $_POST['category']);
          } else {
              $main_smarty->assign('highlight_team_category', true);
          }
          
          // objective existence and length
          if ($team->check_objective_exist() && $team->check_objective_length()) {
              $main_smarty->assign('team_objective', $_POST['objective']);
          } else {
              $main_smarty->assign('highlight_team_objective', true);
          }
          
          // rating
          if ($team->check_rating()) {     
              $main_smarty->assign('team_rating', $_POST['rating']);
              //beceause there is ZERO value also one of select option we need to tell it exists
              $main_smarty->assign('team_rating_selected', true);
          } else {
              $main_smarty->assign('highlight_team_rating', true);
          }
          
          // automatically show team creation form
          $main_smarty->assign('automatic_team_creation_form', true);
          
      }
      
        if ($type=="task"){
          // task name existence and length             
          if ($task->check_task_name_exists() && $task->check_task_name_length() && !$task->duplicate_name()) {  
                    
              $main_smarty->assign('task_name', $_POST['task_name']);
          } else {
              $main_smarty->assign('highlight_task_name', true);
          }
          
          
          // worker
          if ($task->check_task_worker()) {  
              $main_smarty->assign('task_worker', $_POST['task_worker']);
          } else {      
              $main_smarty->assign('highlight_task_worker', true);
              
          }
          
          // deadline
          if ($task->check_task_deadline()) {
              $main_smarty->assign('task_deadline', $_POST['task_deadline']);
          } else {
              $main_smarty->assign('highlight_task_deadline', true);
          }
          
          // description existence and length
          if ($task->check_task_description_exists() && $task->check_task_desciption_length()) {
              $main_smarty->assign('task_description', $_POST['task_description']);
          } else {
              $main_smarty->assign('highlight_task_description', true);
          }
                 
          
          // automatically show task creation form
          $main_smarty->assign('automatic_task_creation_form', true);
          
      }
      
       if ($type=="task_update"){
          // task name existence and length
          if ($task->check_task_name_exists() && $task->check_task_name_length()) {
              $main_smarty->assign('task_name', $_POST['task_name']);
          } else {
              $main_smarty->assign('highlight_task_name_update', true);
          }
          
          
          // worker
          if ($task->check_task_worker()) {
              $main_smarty->assign('task_worker', $_POST['task_worker']);
          } else {
              $main_smarty->assign('highlight_task_worker_update', true);
              
          }
          
          // deadline
          if ($task->check_task_deadline()) {
              $main_smarty->assign('task_deadline', $_POST['task_deadline']);
          } else {
              $main_smarty->assign('highlight_task_deadline_update', true);
          }
          
          // description existence and length
          if ($task->check_task_description_exists() && $task->check_task_desciption_length()) {
              $main_smarty->assign('task_description', $_POST['task_description']);
          } else {
              $main_smarty->assign('highlight_task_description_update', true);
          }
                 
          
          // automatically show task creation form
          $main_smarty->assign('automatic_task_creation_form', true);
          
      }
  }
       //========================================================================
       // Function No_Ohter_Requests() checks if there is another request
       // for team, task or member modifications 
       // @return true if there is no other request otherwise false
       //========================================================================
       function no_other_requests() {
        if (!$_POST['add_member_request'] && !$_POST['update_team_request'] && !$_POST['add_team_task'] && !$_POST['task_completeness_update_request']) {
        return true;
        }
        return false;   
       }
  
  
           
?>
